#include"PictureProcess.h"
/**
INFO:
	to process picture get the plate number
Author:
	Mark on 2014/4/8
	<mark199312@gmail.com>
*/

//PictureProcess::PictureProcess(std::string tName)
//{
//
//}


void PictureProcess::setBG(Background bG)
{
	this->bG=bG;
}

bool PictureProcess::inMarginOfError(int a,int b,int marginError)
{
	//logInfo("process of compare in MarginError");

	if(abs(a-b)<marginError)
	{
		return true;
	}

	return false;
}

vector<Mat> PictureProcess::getPP()
{
	return this->pP;
}

/**
add picture to pP
*/
void PictureProcess::addPicture(Mat &p)
{
	logInfo("start to add the picture");
	pP.push_back(p);

	this->pW=p.size().width;
	this->pH=p.size().height;
}

/**
erase Backgroud through the Background
*/
void PictureProcess::eraseBackground(Mat &m)
{
	logInfo("start to erase the background");

	bool **p;
	p=this->bG.getBMB();
	Mat bGM=this->bG.getBM();

	int sum=0;
	int n=0;

	if(this->pH>m.size().height || this->pW > m.size().width)
	{
		cout<<" the background can not compare to the M";
		return;
	}

	for(int i=0;i<this->pH;i++)
	{
		for(int j=0;j<this->pW;j++)
		{
			//int aaa=1;

			if(p[i][j])
			{
				n++;
				//cout<<i<<" "<<j<<"  "<<n<<endl;

				if(inMarginOfError(bGM.at<Vec3b>(i,j)[0],m.at<Vec3b>(i,j)[0]) && inMarginOfError(bGM.at<Vec3b>(i,j)[1],m.at<Vec3b>(i,j)[1]) && inMarginOfError(bGM.at<Vec3b>(i,j)[2],m.at<Vec3b>(i,j)[2]))
				{
					sum++;
					m.at<Vec3b>(i,j)[0]=0;
					m.at<Vec3b>(i,j)[1]=0;
					m.at<Vec3b>(i,j)[2]=0;
				}
			}
		}
	}

	cout<<n<<endl;
	cout<<sum<<endl;
}

/**
this is to erase the car that go far away in the picture
*/
void PictureProcess::eraseTop(double percentage,Mat &m)
{
	logInfo("start to erase the Top of picture");

	for(int i=0;i<(percentage*m.size().height);i++)
	{
		for(int j=0;j<m.size().width;j++)
		{
				m.at<Vec3b>(i,j)[0]=0;
				m.at<Vec3b>(i,j)[1]=0;
				m.at<Vec3b>(i,j)[2]=0;
		}
	}
}

/**
erase the blue of the picture but sava the green and red
*/

void PictureProcess::eraseRed(Mat &m)
{
	for(int i=0;i<m.size().height;i++)
	{
		for(int j=0;j<m.size().width;j++)
		{
				m.at<Vec3b>(i,j)[0]=0;
		}
	}
}

/**
convert the picture to the Gray
*/

void PictureProcess::convertToGray(Mat &p)
{
	logInfo("start to convertToGray");

	//Mat gray_image;
	cvtColor(p,this->pP[0],CV_BGR2GRAY);
	//p=gray_image;
}

/**
expend the space of brightness
*/

void PictureProcess::brightnessExpend(Mat &p)
{
	logInfo("start to brightnessExpend");
}
/**
0 or 1 to picture
*/

void PictureProcess::threshold(Mat &p)
{
	logInfo("start to threshold");
}
/**
sharp the picture
*/
void PictureProcess::gradientSharp(Mat &p)
{
	logInfo("start to gradientSharp");
}
/**
filter the picture
*/
void PictureProcess::mediaFilter(Mat &p)
{
	logInfo("start to mediaFilter");
}
/**
locate the plate num
*/
void topValue(Mat &p);

PictureProcess::~PictureProcess()
{

}

void PictureProcess::run()
{
	logInfo("PicutureProcess thread run");
}